﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL;
using Framework.Library.Base.Helper;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.Security;
using ServiceStack.Text;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Core.Config;

namespace Framework.Portal
{
    public partial class Popup : BasePopup
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadContent();
        }

        private void LoadContent()
        {
            int controlId = Convert.ToInt32(HttpContext.Current.Request.QueryString["c"]);
            int portalId = Convert.ToInt32(HttpContext.Current.Request.QueryString["p"]);

            if (portalId == 0) portalId = Configurations.PORTALID;
            var infoPortal = Session.CurrentPortal(portalId);
            if (infoPortal == null)
            {
                infoPortal = ManagerFactory.DAOEntity.GetInfo(new PortalPortal() { PORTALID = portalId });
                Session.Add(cfg.PrefixMember.VALUE + "CURRENT_PORTAL_" + portalId, infoPortal);
            }
            if (infoPortal != null)
            {
                if (infoPortal.SA > 0)
                {
                    if (!CredentialHelper.IsValidCredential(HttpContext.Current.Request.UserHostAddress))
                        RedirectHelper.ErrorRedirect("Địa chỉ IP của bạn \"" + HttpContext.Current.Request.UserHostAddress + "\" đã bị chặn !");
                }
                var infoControl = ManagerFactory.DAOEntity.GetInfo<PortalControl>(new PortalControl() { CONTROLID = controlId });
                if (infoControl != null)
                {
                    if (infoControl.SA > 0)
                    {
                        string returnUrl;
                        if (!SecurityHelper.IsAuthenticated(out returnUrl))
                            RedirectHelper.LoginRedirect(returnUrl);                        
                    }
                    string basePath = Configurations.ROOTDIR + "Templates/" + infoPortal.LAYOUT;

                    if (!string.IsNullOrEmpty(infoPortal.THEME))
                    {
                        var infoTheme = JsonSerializer.DeserializeFromString<JTheme>(infoPortal.THEME);
                        if (infoTheme != null)
                        {
                            string templatePath = basePath + infoTheme.popup;
                            TmpControl = (BaseTemplate)LoadControl(templatePath);
                            Page.Title = "(" + cfg.SiteName.VALUE + ") - " + infoControl.CONTROLNAME;
                            RegisterMeta(infoTheme.meta);                           

                            if (!string.IsNullOrEmpty(infoPortal.POPUP))
                            {
                                List<JSection> lstMaster = JsonSerializer.DeserializeFromString<List<JSection>>(infoPortal.MASTERPAGE);
                                if (lstMaster != null)
                                {
                                    if (lstMaster.Count > 0)
                                    {
                                        foreach (var jMaster in lstMaster)
                                        {
                                            if (TmpControl.Containers.ContainsKey(jMaster.placeholder))
                                            {
                                                Control toAdd = LoadControl(jMaster.control, jMaster.title,
                                                                            Convert.ToInt32(controlId), jMaster.parameter);
                                                TmpControl.Containers[jMaster.placeholder].Controls.Add(toAdd);
                                            }
                                        }
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(infoControl.CONTROLPATH))
                            {
                                Control toAdd = LoadControl(infoControl.CONTROLPATH, infoControl.CONTROLNAME,
                                                                                        Convert.ToInt32(controlId));
                                TmpControl.Containers["content"].Controls.Add(toAdd);
                            }
                            plContent.Controls.AddAt(0, TmpControl);
                            for (int i = Controls.Count - 1; i < 0; i--)
                                Controls.RemoveAt(i);
                        }
                    }
                }
                else
                    RedirectHelper.ErrorRedirect("Can't load popup !");
            }
            else
                RedirectHelper.ErrorRedirect("Can't load portal !");
        }
    }
}